package com.ddh.ddhsanguogame.config;

import com.ddh.ddhsanguogame.interceptor.UserInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 配置跨域请求访问
        registry.addMapping("/**")
                .allowedOrigins("*") // 允许的来源
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的请求方法
                .allowedHeaders("*") // 允许的头部信息
                .exposedHeaders("Authorization") // 暴露的头部信息
                .maxAge(3600) // 预检请求的缓存时间（秒）
                .allowCredentials(false); // 是否允许发送凭证
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInterceptor())
                .addPathPatterns("/**")  // 拦截所有请求
                .excludePathPatterns(
                        "/static/**",
                        "/css/**",
                        "/js/**",
                        "/doc.html",  // 排除 Knife4j 的默认访问路径
                        "/v2/api-docs",  // Swagger API 文档
                        "/swagger-resources",  // Swagger 资源
                        "/swagger-resources/configuration/ui",  // Swagger UI 配置
                        "/swagger-resources/configuration/security",  // Swagger 安全配置
                        "/favicon.ico",  // 网站图标
                        "/user/login",  // 登录请求路径
                        "/user/register",  // 注册请求路径
                        "/webjars/**"
                );  // 排除静态资源和其他特定路径
    }
}
